package huashu.net.Investment.until;

import com.sirdc.modules.utils.StringUtils;
import freemarker.template.*;
import huashu.net.Investment.entity.UpdataBean;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Until {

	public static  final  String CODE ="200";
	public static  final String ERROR="500";
	public static  final String  MESSAGE="successful";
	//public static final String APPID = "wxbf15388eb7c5c7f4";//正式
	//public static final String APPSETRET = "26de7eed18826e1795ba5eb4460d4c86";//正式
	public static final String APPID ="wxd813c43f440900d4";//测试
	public static final String APPSETRET="bd049aa84a810e49d9221fe127cc89cc";//测试
	public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";
	public static final String GET_SIGNATURE_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
	//public static final String DOMAIN = "http://huashu.huashitong.net";
	public static final String DOMAIN="http://193135wa08.iask.in";//测试
	
	//获取当当前年月日
	public static String getCurrentDate() {

		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd");
		String currentDate = sf.format(date);

		return currentDate;
	}
	
	//获取当前时间
	public static String getCurrentTime() {
		/*Calendar cal = Calendar.getInstance();
		int hour = cal.get(Calendar.HOUR_OF_DAY);
		int minute = cal.get(Calendar.MINUTE);
		int second = cal.get(Calendar.SECOND);*/
		Date date  = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("HH:mm:ss");
		String time  = sf.format(date);
		return time;
	}
	
	//获取年份 
	public static String getCurrenYear ()
	{
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy");
		String currentYear = sf.format(date);
		return currentYear;
	}
	//获取当前月份
	public static String getCurrentMonth()
	{
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMM");
		String currentMonth = sf.format(date);
		return currentMonth;
	}

	//获取当前日期时间
	public static String getCurrentDateTime(){
		Date date = new Date();
		SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd HH:mm:ss");
		String currnetDateTime = sf.format(date);
		return currnetDateTime;
	}
	
	//属性复制器 add by xxq
	public static void copyPro(Object obj,Object obj2){

		try {
			BeanInfo soursebean = Introspector.getBeanInfo(obj.getClass(), Object.class);
			PropertyDescriptor[] sourpro =soursebean.getPropertyDescriptors();
			
			BeanInfo soursebean2 = Introspector.getBeanInfo(obj2.getClass(), Object.class);
			PropertyDescriptor[] sourpro2 =soursebean2.getPropertyDescriptors();
			
			
			for (int i = 0; i < sourpro.length; i++) {
				for (int j = 0; j < sourpro2.length; j++) {
					if(sourpro[i].getName().equals(sourpro2[j].getName())&&sourpro[i].getPropertyType().equals(sourpro2[j].getPropertyType())  ){
						sourpro2[j].getWriteMethod().invoke(obj2, sourpro[i].getReadMethod().invoke(obj));
						break;
					}
				}
			}
			
		} catch (IntrospectionException e) {
			// TODO Auto-generated catch block
			
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
	
	}
	
	
	public static String isEmpAndTwoDecimal(String str )
	{
		if("".equals( str ) ||str==null){
			return "0";
		}
		DecimalFormat df=new DecimalFormat("#.##");
		Double strDouble =Double.parseDouble(str);
		//strDouble =strDouble/unit;
		return  df.format(strDouble);
		
	}

	private static Configuration configuration = null;
	private static Map<String,Template> allTemple = null;

	static {
		configuration = new Configuration();
		configuration.setDefaultEncoding("UTF-8");

		configuration.setClassForTemplateLoading(Until.class,"huashu/net/Investment/word/");
		configuration.setObjectWrapper(new DefaultObjectWrapper());
		configuration.setTemplateExceptionHandler(TemplateExceptionHandler.IGNORE_HANDLER);

		allTemple =new HashMap<>();
		try {

			allTemple.put("jddjb",configuration.getTemplate("jddjb.ftl"));
		}catch (Exception e)
		{

		}
	}

	private Until(){

	}

	public static File  createDoc(Map<?,?> datamap,String type) throws IOException {
		String name  = "word.doc";
		File file = new File(name);

		Template t = configuration.getTemplate("jddjb.ftl");

		try {
			Writer w = new OutputStreamWriter(new FileOutputStream(file),"utf-8");
			t.process(datamap,w);
			w.close();
		}
		catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}

		return  file;
	}

	/**
	 * 属性比较器
	 */
	public static List<UpdataBean> compareObject(Object obj1, Object obj2) throws ClassNotFoundException,IllegalAccessException
	{
		List<UpdataBean> list  = new ArrayList<UpdataBean>();

		Class<?> cla1 =obj1.getClass();
		Class<?> cla2 =obj2.getClass();

		Field[] field1 =cla1.getDeclaredFields();//获取对象的属性列表
		Field[] field2 =cla2.getDeclaredFields();//

		for(int i =0 ;i<field1.length;i++)
		{
			for(int j =0;j<field2.length;j++)
			{

				if(field1[i].getName().equals(field2[j].getName()))
				{
					if(field1[i].getName().equals(field2[j].getName())){
						field1[i].setAccessible(true);
						field2[j].setAccessible(true);
                        //如果field1[i]属性值与field2[j]属性值内容不相同
						if (!compare(field1[i].get(obj1), field2[j].get(obj2))){
							//Map<String,Object> map2=new HashMap<String, Object>();
							UpdataBean upd  = new UpdataBean();
							/*map2.put("name",field1[i].getName());
							map2.put("old",field1[i].get(obj1));
							map2.put("new",field2[j].get(obj2));*/
							upd.setProperty(field1[i].getName());
							upd.setBeforeupdate(field1[i].get(obj1)==null?"":field1[i].get(obj1).toString());
							upd.setAfterupdate(field2[j].get(obj2)==null?"":field2[j].get(obj2).toString());
							list.add(upd);
						}
						break;
					}
				}

			}


		}
		return list;
	}

	public static boolean compare(Object obj1,Object obj2)
	{

		if(obj1 ==null&&obj2==null)
		{
			return true;
		}
		if(obj1==null&&obj2!=null)
		{
			if(obj1==null&&"".equals(obj2))
			{
				return true;
			}
			if(obj2==null&&"".equals(obj1))
			{
				return true;
			}
			return false;
		}
		if(obj1.equals(obj2))
		{
			return true;
		}

		return false;
	}

	//自动生成下划线
	public static String createUnderLine(String property )
	{
		Pattern pat  = Pattern.compile("[A-Z]");
		if(StringUtils.isBlank(property))
		{
			return "";
		}
		StringBuffer str  = new StringBuffer(property);
		Matcher matcher  = pat.matcher(property);
		int i =0;
		while (matcher.find())
		{
			str.replace(matcher.start()+i,matcher.end()+i,"_"+matcher.group().toLowerCase());
			i++;
		}
		if("_".equals(str.charAt(0) ))
		{
			str.deleteCharAt(0);
		}

		return  str.toString();
	}
}
